package com.jerry.huihome.module.find_near_mine.fragment;

import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find_near_mine.adapter.AfflatusListAdapter;
import com.jerry.huihome.module.find_near_mine.entity.AfflatusListEntity;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by axiang on 2016/5/10.
 */
public class Find_AfflatusFragment extends BaseFragment{
    @BindView(R.id.afflatus_list)
    protected PullToRefreshListView afflatus_view;

    private List<AfflatusListEntity.BodyBean.WorksBean> afflatuslist;
    private AfflatusListAdapter afflatusListAdapter;

    public Find_AfflatusFragment(){
    }

    @Override
    protected int getLayoutId(){
        return R.layout.pulltorefreshview_afflatus;
    }

    @Override
    protected void initViews(){
        afflatuslist = new ArrayList<>();
        afflatusListAdapter = new AfflatusListAdapter(getActivity(), afflatuslist);
        afflatus_view.setAdapter(afflatusListAdapter);
        //获得刷新数据
        afflatus_view.getRefreshableView().setDivider(null);
        //指示色透明
        afflatus_view.getRefreshableView().setSelector(android.R.color.transparent);
        //刷新模式
        afflatus_view.setMode(PullToRefreshBase.Mode.BOTH);

        /*afflatus_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ExpandableListView>(){
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ExpandableListView> refreshView){
                mPagenum =1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ExpandableListView> refreshView){
                mPagenum++;
                loadData();
            }
        });*/
        afflatus_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>(){
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView){
                mPagenum = 1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView){
                mPagenum++;
                loadData();
            }
        });
    }

    private int mPagenum = 1;
    private String reqUrl = Urls.AFFLATUS_URL + "pageIndex=" + mPagenum;

    @Override
    protected void loadData(){
        super.loadData();
       /* Map<String,Object> params = new HashMap<>();
        params.put()*/
        OkHttpUtils.get(reqUrl, new OkHttpUtils.OnGetDataListener(){
            @Override
            public void onResponse(String url, String data){
                //Log.d("data",data);
                //清理:
                if(data != null){
                    if(mPagenum == 1){
                        afflatusListAdapter.getList().clear();
                    }
                    Gson gson = new Gson();
                    AfflatusListEntity afflatusListEntity = gson.fromJson(data, AfflatusListEntity.class);
                   /* if(afflatusListEntity != null & afflatusListEntity.getBody().getWorks() != null &
                            !afflatusListEntity.getBody().getWorks().isEmpty()){*/
                    afflatuslist.addAll(afflatusListEntity.getBody().getWorks());
                    afflatusListAdapter.notifyDataSetChanged();
                    afflatus_view.onRefreshComplete();
                }
            }
            // }

            @Override
            public void onFailure(String url, String error){
            }
        });
    }
}
